<?php
/**
 * @author Feng
 * @date 2012-6-15
 */
class resource_keyword_concept extends system_concept_abstract {
	
	public function init() {
		$this->setRowClass('resource_keyword_row');
		$this->setModel('resource_keyword_model', 'keyword');
		$this->setModelCols('keyword', array('id', 'name', 'is_active', 'hit_total', 'hit_day', 'hit_week', 'hit_month'));
	}
	
	public function save(resource_keyword_row $row) {
		if(!$row->getName()) {
			throw new picture_exception("Keyword Name can not be Empty.");
		}
		$row->setIsActive(($row->get('is_active') ? 1 : 0));
		if($row->getHitTotal() == null) $row->setHitTotal(0);
		if($row->getHitDay() == null) $row->setHitDay(0);
		if($row->getHitWeek() == null) $row->setHitWeek(0);
		if($row->getHitMonth() == null) $row->setHitMonth(0);
		if($row->getId()) {
			if($this->fetch(array('id != ?' => $row->getId(), 'name = ?' => $row->getName()))) {
				throw new picture_exception("Keyword Name is already Existing.");
			}
			$this->getModel('keyword')->update($row->getModelData('keyword'), array('id = ?' => $row->getId()));
			return $row;
		}
		if($this->fetch(array('name = ?' => $row->getName()))) {
			throw new picture_exception("Keyword Name is already Existing.");
		}
		$id = $this->getModel('keyword')->insert($row->getModelData('keyword'));
		$row->setId($id);
		return $row;
	}
	
	public function delete($row) {
		if(!$row) return ;
		$id = $row->getId();
		system_db::getAdapter()->query("DELETE FROM `picture_photo_keyword` WHERE `keyword_id` = '$id'");
		system_db::getAdapter()->query("DELETE FROM `picture_special_keyword` WHERE `keyword_id` = '$id'");
		system_db::getAdapter()->query("DELETE FROM `resource_keyword` WHERE `id` = '$id'");
	}

}
